package org.wmine.algo;

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;


import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Naive extends JFrame
{
	private static final int CENTER = 0;
	private static final int RIGHT = 0;
	int i;String classLbl;
	final JCheckBox [] cb111;
	int [] checkCnt;
	int [] selected;
	
	String[]attribs1;
	int colknt1,cntRow1;String Goalitem;
	String[][]data2;
	public Naive(final String[]attribs,final int colknt,final int cntRow,final String [][] data)
	{
		attribs1=attribs;
		colknt1=colknt;
		cntRow1=cntRow;
		data2=data;
		final Container c=this.getContentPane();
		this.setTitle("Select the Attributes");
		
		JPanel mainPanel=new JPanel();
		mainPanel.setLayout(new GridLayout(colknt+6,1));
		mainPanel.add(new JLabel("Class Label Attribute:"));
		final JComboBox cb=new JComboBox();
		for(int j = 0;j<colknt;j++)
		{
			cb.addItem(attribs[j]);
		}
		cb.addItemListener(new ItemListener() {
		     public void itemStateChanged(ItemEvent e)
		     {
		    	 classLbl=e.getItem().toString();
		    	 
		     }});
		mainPanel.add(cb);
		mainPanel.add(new JLabel(""));
		mainPanel.add(new JLabel("Select the Attributes:"));
		cb111=new JCheckBox[colknt];
		checkCnt=new int[colknt];
		//for(int i = 0;i<colknt;i++)
			//checkCnt[i]=0;
		ActionListener listener=new ActionListener(){public void actionPerformed(ActionEvent ae){update();}};
		for(i = 0;i<colknt;i++)
		{
			cb111[i]=new JCheckBox(attribs[i]);
			/*cb111[i].addActionListener(new ActionListener(){public void actionPerformed(ActionEvent ae)
			{
				//System.out.println(i+" "+checkCnt[i]);
			   // checkCnt[i]++;
				//call();
				//updateCnt(i);
			}}); 
			*/
			cb111[i].addActionListener(listener);
			mainPanel.add(cb111[i]);
		}
		mainPanel.add(new JLabel(""));
		//mainPanel.add(new JLabel(""));
		JButton but1=new JButton("OK");
		//JButton but2=new JButton("Cancel");
		but1.setBounds(10,45,110,20);
		//but2.setBounds(140,45,110,20);
		mainPanel.add(but1);
		//mainPanel.add(but2);
		but1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent ae)
		{
			dispose();
			selected=new int[cb111.length];
			Goalitem=(String) cb.getSelectedItem();
			for(int s=0;s<cb111.length;s++)
			{
			//	if(checkCnt[s]%2==0)selected[s]=0;
				//else selected[s]=1;
				get(s);
				
				/*if(ok==1)selected[s]=1;
				else selected[s]=0;*/
					
			}
			
			callNaive1();
			//Naive1 sd1=new Naive1(attribs,colknt,cntRow,data,selected,classLbl);
			//sd1.setVisible(true);
		}});
	
		
		c.add(mainPanel);
		this.setBounds(300,200,300,(30*(colknt+1)));
	
	}
	public static void main(String [] args)
	{
		String []s={"agsdfj","bsdgd","gfadger","watwt34"};
		String [][]ss=new String[3][2];
		/*Naive sd=new Naive(s,4,ss);
		sd.setVisible(true);*/
	}
	void update()
	{
	//	checkCnt[i]++;
	}
	void get(int i)
	{
		if(Goalitem.equals(attribs1[i]))
			{System.out.print(Goalitem);selected[i]=2;}
		else if(cb111[i].isSelected())
		{
			System.out.print("ok");
			selected[i]=1;
		}
		else
		selected[i]=0;
	
		
	}
	void callNaive1()
	{
		System.out.print("great");
		Naive1 sd1=new Naive1(attribs1,colknt1,cntRow1,data2,selected,classLbl);
		sd1.setVisible(true);
	}
}

class Naive1 extends JFrame
{
	private static final int CENTER = 0;
	private static final int RIGHT = 0;
	int i,rowCnt;
	String [][]data1;
	int goalIndex,attribKnt=0;
	int Ind=0;
	int [][] vMat;
	double [] vProb;
	Vector vName = new Vector();
	Vector vCnt = new Vector();
	Vector vCount = new Vector();
	final JLabel [] lb1;
	int [] indOrder;
	final JComboBox [] cb1;
	public Naive1(final String[]attribs,int colknt,final int cntRow,String [][] data,int[]selected,String classLbl)
	{
		rowCnt=cntRow;
		data1=data;
	
		/*for(int k=0;k<selected.length;k++)
		{
			if(attribs[k].equals(classLbl))selected[k]=2;			
		}*/
		
		for(int k=0;k<selected.length;k++)
		{
			if(selected[k]==1){
				//classify(k);
				System.out.println(""+attribs[k]);
				attribKnt++;
				}
			else if(selected[k]==2){
				//classify(k);
				goalIndex=k;
				}
		}
		////////////////////////////////////
		final Container c=this.getContentPane();
		this.setTitle("Statistical Analysis");
		
		JPanel mainPanel=new JPanel();
		mainPanel.setLayout(new GridLayout(attribKnt+3,1));

		lb1=new JLabel[attribKnt];
		cb1=new JComboBox[attribKnt];
		indOrder=new int[attribKnt];
		mainPanel.add(new JLabel(""));
		mainPanel.add(new JLabel(""));
		Ind=0;
		//System.out.println(attribs[0]);
		for(int iii = 0;iii<colknt;iii++)
		{
			if(selected[iii]==1)
			{
				indOrder[Ind]=iii;
				lb1[Ind]=new JLabel(attribs[iii]);
				lb1[Ind].setHorizontalAlignment(RIGHT);
				//lb1[i].setHorizontalAlignment(CENTER);
				cb1[Ind]=new JComboBox();
			//	cb1[Ind].addItem("- unspecified -");
				cb1[Ind].setMaximumRowCount(5);
				Vector v=classify(iii);
				for(int y=0;y<v.size();y++)
					cb1[Ind].addItem(v.get(y));
				
				//cb1[i].setSize(6,30);
				mainPanel.add(lb1[Ind]);
				mainPanel.add(cb1[Ind]);
				Ind++;
			}
		}
		System.out.println("Max Ind "+Ind);
		mainPanel.add(new JLabel(""));
		mainPanel.add(new JLabel(""));
		JButton but1=new JButton("Process");
		JButton but2=new JButton("Cancel");
		but1.setBounds(10,45,110,20);
		but2.setBounds(140,45,110,20);
		mainPanel.add(but1);
		mainPanel.add(but2);
		but1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent ae)
		{
			dispose();
			vName=classify(goalIndex);
			fillMatrix();
			fillProb();
			int max=findMaxProb();
			String ans="The Naive Bayesian Classifier predicts '"+attribs[goalIndex]+"' = '"+vName.elementAt(max).toString()+"'";
			JOptionPane.showMessageDialog(null, ans);
			//ProgressBar pb=new ProgressBar();
			//pb.getDialog();
		}});
	
		
		c.add(mainPanel);
		this.setBounds(300,200,300,(30*(colknt+1)));
	
	}
	public int findMaxProb()
	{
		double max;int ind=-1;
		if(vProb.length!=0)
			{max=vProb[0];ind=0;}
		else return -1;
		
		for(int i=0;i<vProb.length;i++)
		{
			if(vProb[i]>max)
			{
				max=vProb[i];
				ind=i;
			}
		}
		return ind;
	}
	public void fillProb()
	{
		vProb=new double[vName.size()];
		double val=1;
		for(int j=0;j<vName.size();j++)
		{
			val=1;
			for(int r=0;r<Ind;r++)
			{
				val*=(vMat[r][j]/Double.parseDouble(vCount.elementAt(j).toString()));
			}
			val*=(Double.parseDouble(vCount.elementAt(j).toString())/rowCnt);
			vProb[j]=val;
			System.out.println(""+vName.elementAt(j).toString()+","+vProb[j]);
		}
	}
	public void fillMatrix()
	{
		vMat=new int[Ind][vName.size()];
		int val=0;
		for(int i=0;i<Ind;i++)
		{
			for(int j=0;j<vName.size();j++)
			{
				val=0;
				for(int r=0;r<rowCnt;r++)
				{
					if((data1[r][indOrder[i]].equals(cb1[i].getSelectedItem().toString()))&&(data1[r][goalIndex].equals(vName.elementAt(j).toString())))
					{
						val++;
					}
				}
				vMat[i][j]=val;
				System.out.print("(i,j):val"+i+" "+j+""+vMat[i][j]);
			}
		}
	}
	public Vector classify(int index)
	{	
		int size=0;	int ind;
		Vector vt=new Vector();
		vCnt=new Vector();
		for(int o=0;o<rowCnt;o++)
		{
			if(vt.size()!=0)
			{
				if((ind=vt.indexOf(data1[o][index]))!=-1)
				{
					int c=Integer.parseInt(vCnt.elementAt(ind).toString());
					c++;
					vCnt.insertElementAt(c, ind);
					//vCnt.insertElementAt(new String(""+c+""), ind);
				}
				else
				{
					vt.insertElementAt(data1[o][index], size);
					vCnt.insertElementAt(1, size);
					size++;
				}
			}
			else
			{
					vt.insertElementAt(data1[o][index], size);
					vCnt.insertElementAt(1, size);
					size++;
			}			
		}
		System.out.println("No.of Classes:"+vt.size());
		for(int y=0;y<vt.size();y++)
			System.out.println("Class "+(y+1)+":"+vt.get(y)+" >> "+vCnt.get(y));
		if(index==goalIndex)
		{
			vCount=vCnt;
		}
		return vt;
		
	}
	public static void main(String [] args)
	{
		String []s={"agsdfj","bsdgd","gfadger","watwt34"};
		String [][]ss=new String[3][2];
		//Naive sd=new Naive(s,4,1,ss);
		//sd.setVisible(true);
	}
}